package com.jumi.microservice.material.service.impl;

import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.common.exception.ExceptionEnum;
import com.jumi.microservice.material.domain.JmCategory;
import com.jumi.microservice.material.dto.category.JmCategoryResponse;
import com.jumi.microservice.material.mapper.JmCategoryMapper;
import com.jumi.microservice.material.service.JmCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ssk
 * @create 2020/7/17 9:34
 **/
@Service
public class JmCategoryServiceImpl implements JmCategoryService {


    @Resource
    private JmCategoryMapper jmCategoryMapper;

    /*
     *
     * @param jmCategory
     * @return
     *   -1:已经存在
     *
     * */
    @Override
    public int saveJmCategory(JmCategory jmCategory) {
        int hasSame = jmCategoryMapper.findCount(jmCategory);
        if (hasSame != 0) {
            throw new BaseException(ExceptionEnum.FAIL_CATEGORY_RXIST);
        } else {
            return jmCategoryMapper.save(jmCategory);
        }
    }
    /*
    * 查询总条数
    * */
    @Override
    public int findCount(JmCategory jmCategory) {
        return  jmCategoryMapper.findCount(jmCategory);
    }

    /*
    * 更新数据
    * */
    @Override
    public int updateJmCategory(JmCategory jmCategory) {
        int hasSame = jmCategoryMapper.findCount(jmCategory);
        if (hasSame != 0) {
            throw new BaseException(ExceptionEnum.FAIL_CATEGORY_RXIST);
        } else {
            int getback = jmCategoryMapper.update(jmCategory);
            return getback;
        }
    }


    @Override
    public JmCategory findById(Long id) {

        JmCategory listOne = jmCategoryMapper.findById(id);

        return listOne;
    }

    @Override
    public List<JmCategory> findAll(JmCategory jmCategory) {
        List<JmCategory> listAll = jmCategoryMapper.findAll(jmCategory);
        return listAll;
    }

    @Override
    public int deleteById(JmCategory jmCategory) {
        int getBack = jmCategoryMapper.deleteById(jmCategory);
        return getBack;
    }

    @Override
    public int saveAndUpdate(JmCategory jmCategory) {
        return 0;
    }

    @Override
    public int deleteBatchByIds(JmCategory jmCategory) {
        int getDelNum = jmCategoryMapper.deleteBatchByIds(jmCategory);
        return  getDelNum;
    }


    @Override
    public int statusById(JmCategory jmCategory) {
        int getBack = jmCategoryMapper.statusById(jmCategory);
        return getBack;
    }

    @Override
    public List<JmCategoryResponse> findByCategoryIds(List<Long> categoryIds) {
        return jmCategoryMapper.findByCategoryIds(categoryIds);
    }
}
